<?php session_start();?>
<html>
<head>
	<title>Survey</title>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" />
	<meta name="format-detection" content="telephone=no" />
	
	<link rel="stylesheet" href="css/style.css" />
	
	<!--<link rel="stylesheet" href="calendar/calendar-win2k-cold-1.css" />
	<script type="text/javascript" src="calendar/calendar.js"></script>
	<script type="text/javascript" src="calendar/calendar-en.js"></script>
	<script type="text/javascript" src="calendar/calendar-setup.js"></script>-->
	
	<link rel="stylesheet" href="calendar2/CalendarControl.css" />
	<script type="text/javascript" src="calendar2/CalendarControl.js"></script>
	
</head>
<body>
	<form action="confirm.php" method="post">
	<select name="company" id="company">
<?php
include 'config.php';

$comp_id = $_SESSION['comp_id'];

$conn = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db("mweb_survey");

$sql_comp = "SELECT * FROM company";
$result_comp = mysql_query($sql_comp, $conn) or die(mysql_error());

while ($row_comp = mysql_fetch_assoc($result_comp)) {
	$row_comp_id = $row_comp['CompID'];
	$row_comp_name = $row_comp['CompName'];
	if ($row_comp_id == $comp_id) {
		echo "<option selected='selected' value='$row_comp_id'>$row_comp_name</option>";
	} else {
		echo "<option value='$row_comp_id'>$row_comp_name</option>";
	}
}
?>
	</select>
	<br/>
	<input type="submit" value="Submit" />
	</form>

<?php
if (isset($_REQUEST['comp_id'])) {
	$comp_id = $_REQUEST['comp_id'];
	$_SESSION['comp_id'] = $comp_id;
}

$sql_comp = "SELECT * FROM company WHERE CompID='$comp_id'";
$result_comp = mysql_query($sql_comp, $conn) or die(mysql_error());

$row_comp = mysql_fetch_assoc($result_comp);
$comp_name = $row_comp['CompName'];
$comp_type = $row_comp['Type'];
$comp_sector = $row_comp['Sector'];
$comp_sub_sector = $row_comp['Subsector'];
$comp_addr = $row_comp['StreetAddr'] . ", " . $row_comp['City'] . ", " . $row_comp['State'] . ", " . $row_comp['Zip'];
$comp_phone = $row_comp['Phone'];
$comp_note = $row_comp['Notes'];
echo "Company Name: $comp_name<br/>";
echo "Sector: $comp_sector<br/>";
echo "Subsector: $comp_sub_sector<br/>";
echo "Company Type: $comp_type<br/>";
echo "Address: $comp_addr<br/>";
echo "Phone: <a href='tel:$comp_phone'>$comp_phone</a><br/><br/>";

echo "<form action='answer.php' method='post'>";
// echo "<button onclick=\"window.location.href='question.php'\">OK</button>";

$sql_grp = "SELECT * FROM ques_grp";
$result_grp = mysql_query($sql_grp, $conn) or die(mysql_error());

while ($row_grp = mysql_fetch_assoc($result_grp)) {
	$grp_id = $row_grp['QuesGrpID'];
	$grp_name = $row_grp['QuesGrpName'];
	echo "<b><u>$grp_name</u></b><br/>";
	
	$sql_ques = "SELECT * FROM questions WHERE QuesGrpID='$grp_id' ORDER BY QuesID";
	$result_ques = mysql_query($sql_ques, $conn) or die(mysql_error());
	
	$i = 1;
	
	while ($row_ques = mysql_fetch_assoc($result_ques)) {
		$ques_id = $row_ques['QuesID'];
		$ques_type = $row_ques['QuesType'];
		$ques = $row_ques['Question'];
		echo "<br/>&nbsp;$i.&nbsp;$ques<br/>";
		$i++;
		if ($ques_type == 'Textfield') {
			if ($ques_id == 2610) {
				// echo "<input type='text' name='textfield$grp_id$ques_id' id='dt' /><img src='calendar/img.gif' id='f_trigger_c' style='cursor: pointer; 	border: 1px solid red;' title='Date selector' /><br/>";
				echo "<input type='text' name='textfield$grp_id$ques_id' onfocus='showCalendarControl(this);' /><br/>";
			} else {
				echo "<input type='text' name='textfield$grp_id$ques_id' /><br/>";
			}
		}
		if ($ques_type == 'Radio') {
			$sql_choice = "SELECT * FROM ques_choices WHERE QuesID='$ques_id'";
			$result_choice = mysql_query($sql_choice, $conn) or die(mysql_error());
			
			while ($row_choice = mysql_fetch_assoc($result_choice)) {
				$choice = $row_choice['choice_value'];
				$default = $row_choice['Default'];
				if ($default=='1') {
					echo "<input type='radio' name='radio$grp_id$ques_id' value='$choice' checked='yes' />$choice<br/>";
				} else {
					echo "<input type='radio' name='radio$grp_id$ques_id' value='$choice'/>$choice<br/>";
				}
			}
			echo "<br/>";
		}
		
		if ($ques_type == 'Checkbox') {
			$sql_choice = "SELECT * FROM ques_choices WHERE QuesID='$ques_id'";
			$result_choice = mysql_query($sql_choice, $conn) or die(mysql_error());
			
			while ($row_choice = mysql_fetch_assoc($result_choice)) {
				$choice = $row_choice['choice_value'];
				$checkname = "checkbox" . $grp_id . $ques_id . "[]";
				echo "<input type='checkbox' name='$checkname' value='$choice'/>$choice<br/>";
			}
			echo "<br/>";
		}
		
		if ($ques_type == 'Dropdown') {
			$sql_choice = "SELECT * FROM ques_choices WHERE QuesID='$ques_id'";
			$result_choice = mysql_query($sql_choice, $conn) or die(mysql_error());
			
			echo "<select name='dropdown$grp_id$ques_id'>";
			while ($row_choice = mysql_fetch_assoc($result_choice)) {
				$choice = $row_choice['choice_value'];
				echo "<option value='$choice'/>$choice</option>";
			}
			echo "</select><br/>";
		}
	}
	echo "<br/>";
}
echo "Comment: <br/>";
echo "<textarea rows='10' cols='30' name='comment'></textarea><br/><br/>";
echo "<input type='submit' value='Submit'/>";
echo "</form>";
// echo <<< HERE
// <script type="text/javascript"> 
    // Calendar.setup({
        // inputField     :    "dt",     // id of the input field
        // ifFormat       :    "%B %e, %Y",      // format of the input field
        // button         :    "f_trigger_c",  // trigger for the calendar (button ID)
        // align          :    "Tl",           // alignment (defaults to "Bl")
        // singleClick    :    true
    // });
// </script>
// HERE;
?>
<?php include 'footer.php';?>
</body>
</html>